/*
	Programação Concorrente
	Trabalho 3
	Turma A

	Adilson Abreu Couto Rapozo Junior ( n. USP: 6402336 )
	Luiz Alfredo Bertoni ( n. USP: 6418834 )
	Rodrigo Fernandes da Costa ( n. USP: 6793073 )
*/


#include "main.h"


double dabs(double a) {
    return (a < 0) ? (-1.0f)*a : a;
}


double jacobiError(double * k_plus, double * k, int size) {
    
    int i;
    double aux = 0.0f;
    double dif = 0.0f;
    
    for(i = 0; i < size; i++) {
        
        if(dabs(k_plus[i]) != 0.0f)
            aux = dabs((k_plus[i] - k[i])/k_plus[i]);
        
        if(dif < aux)
            dif = aux;
    }
    
    return dif;
}


void jacobi(strut * sis, double * k1, double * k0, int pbegin, int pend) {
   
    double aux;
    
    int i, j;
    
    for(i = pbegin; i < pend; i++) {
    
        aux = 0.0f;
        
        for(j = 0; j < i; j++)
            aux += k0[j] * sis->matrix[i][j];
        
        for(j = i + 1; j < sis->J_ORDER; j++)
            aux += k0[j] * sis->matrix[i][j];
        
        k1[i - pbegin] = (sis->matrix[i][sis->J_ORDER] - aux) / sis->matrix[i][i];
    }
}

